<?php

namespace App\Kuafu\Export\Service\ExportFactory;

use App\Kuafu\Shop\Model\SourceMaterialTemplateModel;
use App\Kuafu\Shop\Service\ToolsService;

/**
 * app-创建物料素材模版表
 */
class CreateSourceMaterialTemplateService extends BaseExportService
{
    private string $localPath = '';
    private string $tempPath  = '';

    public function __destruct()
    {
        // 清理临时文件
        if ($this->tempPath) {
            $fileSystem = getFileSystem();
            if ($fileSystem->isDirectory($this->tempPath)) {
                $fileSystem->deleteDirectory($this->tempPath);
            }
        }
    }

    public function query(): self
    {
        $shopId             = (int)($this->params['shopId'] ?? 0);
        $info               = $this->params['templateInfo'];
        $createTemplateData = $this->params['createTemplateData'];
        $userId             = $this->params['userId'];
        $imgType            = $this->params['imgType'];

        $res             = (new ToolsService())->generateTemplateImg($info, $createTemplateData, $userId, $shopId);
        $this->localPath = $res['upright'] ?? "";//正面图
        if ($imgType == SourceMaterialTemplateModel::TEMPLATE_IMG_TYPE_REVERSE) {
            $this->localPath = $res['reverse'] ?? "";//背面图
        }
        $this->tempPath = $res['templateBasePath'] ?? "";//返回文件存储临时目录
        return $this;
    }

    public function path(): string
    {
        return $this->upload($this->localPath);
    }
}
